//
//  EditWeightViewController.m
//  WeightMaster
//
//  Created by maoyu on 15/8/18.
//  Copyright (c) 2015年 buddysoft. All rights reserved.
//

#import "EditWeightViewController.h"
#import "BDFoundation.h"
#import "WMAppSetting.h"
#import "WMDataCache.h"
#import "WMWeight.h"
#import "DataManager.h"

#define ZEOR_BUTTON_TAG 10
#define DOT_BUTTON_TAG  11
#define DEL_BUTTON_TAG  12

@interface EditWeightViewController ()

@end

@implementation EditWeightViewController

- (void)initView {
    CGFloat fontSize = 55.0;
    if (320 == kScreen_Width) {
        fontSize = 30.0;
    }
    
    self.weightLabel.text = @"";
    self.weightLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:fontSize];
    
    UIButton * button;
    for (NSInteger index = 1;index <= 12;index++) {
        button = (UIButton *)[self.view viewWithTag:index];
        button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:fontSize];
        [button setTitleColor:RGB(89, 82, 102) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self.finishButton makeCornorRadius:17.0];
    self.finishButton.enabled = NO;
    
    if (self.editMode) {
        [self.finishButton setTitle:@"保存" forState:UIControlStateNormal];
    }
}

- (void)clickButton:(UIButton *)sender {
    NSString * weight = self.weightLabel.text;
    NSInteger length = weight.length;
    if (length == 5 && DEL_BUTTON_TAG != sender.tag) {
        return;
    }
    
    if (1 <= sender.tag && 9 >= sender.tag) {
        weight = [weight stringByAppendingString:sender.titleLabel.text];
    }else if (DEL_BUTTON_TAG == sender.tag) {
        if (0 < length) {
            weight = [weight substringToIndex:length - 1];
        }
    }else if (DOT_BUTTON_TAG == sender.tag) {
        if (0 < length && NO == [weight hasSuffix:sender.titleLabel.text]) {
            weight = [weight stringByAppendingString:sender.titleLabel.text];
        }
    }else if (ZEOR_BUTTON_TAG == sender.tag) {
        if (0 < length) {
            weight = [weight stringByAppendingString:sender.titleLabel.text];
        }
    }
    
    self.weightLabel.text = weight;
    if (0 == weight.length) {
        self.finishButton.enabled = NO;
    }else {
        self.finishButton.enabled = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString * title = nil;
    if (self.editMode) {
        title = @"请输入目标体重";
    }else{
        title = @"输入您当前体重";
    }
    
    NSString * unit = [WMHelper stringValueForUnit:[WMAppSetting sharedInstance].weightUnit];
    self.title = [NSString stringWithFormat:@"%@（%@）", title, unit];

    
    self.widthConstraint.constant = 65*kScreen_Width/375;
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)finish:(id)sender {
    NSString * weight = self.weightLabel.text;
    NSInteger length = weight.length;
    
    if (YES == [weight hasSuffix:@"."]) {
        NSRange range = [weight rangeOfString:@"."];
        if (length - 1 == range.location) {
            weight = [weight substringToIndex:length - 1];
        }
    }
    
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *tmpWeight = [numberFormatter numberFromString:weight];

    if ([WMAppSetting sharedInstance].weightUnit.integerValue == WMWeightUnitJin) {
        tmpWeight = @(tmpWeight.floatValue / 2);
    }
    
    if (self.editMode) {
        [WMAppSetting sharedInstance].goalWeight = tmpWeight;
        [[WMAppSetting sharedInstance] syncData];
        
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        WMWeight * wmWeight = [[WMWeight alloc] init];
        wmWeight.value = tmpWeight;
        wmWeight.measuringDate = [NSDate date];
        
        [[WMAppSetting sharedInstance] syncData];
        [[WMDataCache sharedInstance] addWeight:wmWeight fromCloud:NO];
        [[DataManager defaultInstance] setAppSetFinished];
        
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
